<?php

//namespace application\widgets;

use \CWidget;

/**
 * CDialog.
 *
 * This file is the widget for the common Moco dialog 
 * It allows to display the popup with any content . Is analogue of the CJuiDialog, but allows the any layout and loading with the url.
 *
 * To init the dialog widget, please put the valid parameters
 * 
 * You can also put the base content within the widget body before the endWidget() function call or set the 'url' parameter, to set the dialog's content onLoad
 *
 * @author Sysprog <sysprog@martis-ua.com>
 * @link http://www.tmx.com.ua/
 * @copyright Copyright &copy; 2011 
 * @license http://www.tmx.com.ua/license/
 * @version 1.0.0.1 $
 */
 
 class CDialog extends \CWidget
 {
	/**
	 * @var string identificator of the dialog
	 */
	public $Id = "";
	
	/**
	 * @var array the list of used labels
	 *
	 * <ul>
	 * <li>title : base title of the dialog. Could be also set by $this->title</li>
	 * <li>close : text of the close button</li>
	 * <li>body  : text of the body</li>
	 * </ul>
	 */
	public $labels = array();
	
	/**
	 * @var string the title of the dialog. Replaces the $this->labels[title]
	 */
	public $title = '';
	
	/**
	 * @var boolean whether to show dialog modal
	 */
	public $modal = true;
	

	/**
	 * @var string the url where content is loaded
	 * See {@link CHtml::normalizeUrl} for possible Url formats
	 */
	public $url = '';

	/**
	 * @var integer width of the popup dialog
	 */
	public $width;

	/**
	 * @var integer height of the popup dialog
	 */
	public $height;

	/**
	 * @var string the client side function would be called after the dialog close. Format: function() { body }
	 */
	public $onClose = '';

	/**
	 * @var string the client side function would be called after the dialog content is loaded $this->onLoad. Format: function() { body }
	 */
	public $onLoad = '';
	
	public $position = "top";
	
	public $showOnLoad = false;

	public $initClientScript = true;

	public $options = array();

	public function init()
	{
		$id = $this->getId();
		if($id == '')
		{
			$id = rand(5, 10);
		}

		/*if($this->url !== null)
		{
			$this->url = CHtml::normalizeUrl($this->url);
		}*/

		if($this->width < 1)
		{
			$this->width = 700;
		}

		if($this->height < 1)
		{
			$this->height = 400;
		}

		$options = $this->getOptions();
		$options['id'] = $id;

		$cs = Yii::app()->getClientScript();

		if($this->initClientScript)
		{
			$jsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.widgets.js'), false, -1, YII_DEBUG);
			$cs->registerScriptFile($jsUrl."/dialog.js");
			$cssUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.widgets.css'), false, -1, YII_DEBUG);
			$cs->registerCssFile($cssUrl."/dialog.css");
		}

		$onLoad = '';
		if($this->onLoad != '')
		{
			$onLoad = $this->onLoad;
			if(!preg_match('/function/', $onLoad))
			{
				$onLoad = "function() { ".$onLoad." } ";
			}
		}

		$onClose = '';
		if($this->onClose != '')
		{
			$onClose = $this->onClose;
			if(!preg_match('/function/', $onClose))
			{
				$onClose = 'function () { '.$onClose." } ";
			}
		}

		$grayBlock = '<div id="grayBlock" class="fix_scroll_wrap fixed" style="opacity:0.7; ';
		
		if(!$options["showOnLoad"])
		{
			$grayBlock .= 'display:none;';
		}

		$grayBlock .= 'height:100%; background-color:#656565;width:100%;opacity:70%;" onClick="$.moco.dialogs.hide(); "></div>';

		$script = ' if( $.moco.dialogs.count == 0 ) { $("body").append(\''.$grayBlock.'\'); } ';

		$script .= ' $.moco.dialogs.add(null, 
					{ id : "dialog-'.$id.'", url : "'.$options['url'].'", width : "'.$options['width'].'", height: "'.$options['height'].'", position : "'.$options['position'].'", showOnLoad : '.(isset($options["showOnLoad"]) ? $options["showOnLoad"] : 'false').', modal : "'.(isset($options["modal"]) ? $options["modal"] : "false").'" }); ';

		if(strlen($onLoad) > 3)
		{
			$script .= ' $.moco.dialogs["dialog-'.$id.'"].onLoad = '.$onLoad.'; ';
		}

		if(strlen($onClose) > 3)
		{
			$script .= ' $.moco.dialogs["dialog-'.$id.'"].onClose = '.$onClose.'; ';
		}

		$cs->registerScript('Moco.Dialog#'.$id, $script);

		$this->render('dialog', array('options' => $options));
	}

	public function getOptions()
	{
		$options = $this->options;

		foreach(array('labels', 'url', 'width', 'height', 'onClose', 'onLoad', 'position', 'modal', 'showOnLoad') as $name)
		{
			if($this->$name != null) 
			{
				$options[$name] = $this->$name;
			}
			else
			{
				$options[$name] = null;
			}
		}

		if( $this->title != "")
		{
			$options['labels']['title'] = $this->title;
		}

		return $options;
	}

	public function run()
	{
		echo "</div></div>";
	}
 }
 
?>